package com.zt.bus.order.controller;

import com.zt.bus.constant.BusConstant;
import com.zt.bus.order.model.OrderBackInfo;
import com.zt.bus.order.service.OrderAmountService;
import com.zt.bus.order.service.OrderBackService;
import com.zt.servlet.FieldAlias;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.system.model.BusinessException;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/orderBack")
public class OrderBackController {

    @Autowired
    private OrderBackService orderBackService;
    @Autowired
    private OrderAmountService orderAmountService;

    /**
     * 查询
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryOrderBack(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderBackService.getTable(reqBean));
    }
    /**
     * 退订订单
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/getOrderBackByPhone")
    @ResponseBody
    public JSONObject getOrderBackByPhone(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(orderBackService.getOrderBackByPhone(reqBean.getUserId()));
    }
    
    /**
     * 获取退订订单详情
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/getOrderBackDetailById")
    @ResponseBody
    public JSONObject getOrderBackDetailById(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	String orderBackId = reqBean.getInfo("orderBackId");
        return Response.response(orderBackService.getOrderBackDetailById(orderBackId));
    }
    /**
     * 获取可退金额
     * 
     * @param reqBean
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getCanRefundAmount")
    @ResponseBody
    public JSONObject getCanRefundAmount(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        return Response.response(orderAmountService.getCanRefundAmount(reqBean.getInfo("orderId"),
                getOrderBackInfoList(reqBean.getInfo("backInfos"))));
    }

    /**
     * 申请退款
     * 
     * @param reqBean
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/apply")
    @ResponseBody
    public JSONObject apply(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        orderBackService.apply(reqBean.getInfo("orderId"), reqBean.getInfo("reason"), reqBean.getInfo("orderBackType"),
                getOrderBackInfoList(reqBean.getInfo("backInfos")));
        return Response.responseSuccess();
    }

    /**
     * 退款申请通过
     * 
     * @param reqBean
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/agree")
    @ResponseBody
    public JSONObject agree(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        return Response.responseSuccess(orderBackService.agree(reqBean.getInfo("orderBackId")));
    }

    /**
     * 退款申请驳回
     * 
     * @param reqBean
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/reject")
    @ResponseBody
    public JSONObject reject(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        orderBackService.reject(reqBean.getInfo("orderBackId"));
        return Response.responseSuccess();
    }
    
    private List<OrderBackInfo> getOrderBackInfoList(String backInfos) {
        if (StringUtil.isEmpty(backInfos)) {
            throw new BusinessException("请求参数错误");
        }
        List<OrderBackInfo> list = new ArrayList<>();
        JSONArray array = JSONArray.fromObject(backInfos);
        for (int i = 0; i < array.size(); i++) {
            OrderBackInfo orderBackInfo = new OrderBackInfo();
            JSONObject json = array.getJSONObject(i);
            orderBackInfo.setPassengerName(json.getString("passengerName"));
            orderBackInfo.setPassengerPhone(json.getString("passengerPhone"));

            List<String> dates = new ArrayList<>();
            JSONArray dateArray = json.getJSONArray("dates");

            for (int j = 0; j < dateArray.size(); j++) {
                JSONObject date = dateArray.getJSONObject(j);
                dates.add(date.getString("date"));
            }
            orderBackInfo.setDates(dates);
            list.add(orderBackInfo);
        }
        return list;
    }

}
